package com.qumitech.visualnovel.controller.rest;

import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.pubdef.dto.retain.RetainMessageDTO;
import com.qumitech.visualnovel.service.retain.RetainMessageService;
import com.google.common.collect.ImmutableMap;
import org.hibernate.annotations.Parameter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/retain_message")
public class RetainMessageController {

    @Autowired
    RetainMessageService retainMassageService;

    @GetMapping(value = "")
    public BaseRsp listRetainMessages(@RequestParam(value = "type", required = false) Integer type,
                                      @RequestParam(value = "title", required = false) String title,
                                      @RequestParam(value = "content", required = false) String content,
                                      @RequestParam(value = "orderBy", defaultValue = "create_time") String orderBy,
                                      @RequestParam(value = "orderType", defaultValue = "desc") String orderType,
                                      @RequestParam(value = "offset", defaultValue = "0") int offset,
                                      @RequestParam(value = "limit", defaultValue = "20") int limit) {
        List<HashMap<String, Object>> data = retainMassageService.listRetainMessages(type, title, content, orderBy, orderType, offset, limit);
        int count = retainMassageService.countRetainMessages(type, title, content);
        return new BaseRsp(ResultEnum.SUCCESS, ImmutableMap.of("count", count, "items", data));
    }

    @PostMapping(value = "")
    public BaseRsp updateRetainMessage(@RequestBody RetainMessageDTO retainMessageDTO) {
        retainMassageService.updateRetainMessages(retainMessageDTO);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

    @DeleteMapping(value = "")
    public BaseRsp deleteRetainMessage(@RequestParam(value = "id") Integer id) {
        retainMassageService.deleteRetainMessage(id);
        return new BaseRsp(ResultEnum.SUCCESS);
    }

}
